package com.itheima.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
//import entity.QueryPageBean;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    //findAll
    @Override
    public List<Brand> findAllBrand() {
//        List<Brand> brandList = this.list();
        return this.list();
    }

    //add
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public Result addBrand(Brand brand) {
        boolean save = this.save(brand);
        if (save) {
            //true
            return new Result(true, 200, "添加成功", null);
        } else {
            //false
            return new Result(false, 400, "添加失败", null);
        }
    }

//    //findPage(change)
//    @Override
//    public PageResult<Brand> findPage(Integer page, Integer size) {
//
//        IPage<Brand> brandPage = new Page<>(page, size);
//        page(brandPage, new QueryWrapper<Brand>()
//                .like("name", queryString));
//
//        return new PageResult<>(brandPage.getTotal(), brandPage.getRecords());
//    }

    //findPage
    @Override
    public PageResult<Brand> findAll(Integer currentPage, Integer pageSize, String queryString) {

//        List<Brand> brandList = this.list();
        IPage<Brand> brandPage = new Page<>(currentPage, pageSize);
        page(brandPage, new QueryWrapper<Brand>()
                .like("name", queryString));
//        brandPage.setRecords(brandList);
//        brandPage.setCurrent(currentPage);
//        brandPage.setSize(pageSize);
//        QueryPageBean<Brand> queryPageBean = new QueryPageBean<>(brandPage.getSize(), brandPage.getRecords());
//        return new ResultInfo(true, queryPageBean, null);
        return new PageResult<>(brandPage.getTotal(), brandPage.getRecords());
    }

    //deleteBrand
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBrand(Integer id) {
        // 根据 ID 删除
        boolean removeById = this.removeById(id);
        if (removeById) {
            return new Result(true, 200, "删除成功", null);
        } else {
            return new Result(false, 400, "删除失败", null);
        }
    }

    //updateBrand
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public Result updateBrand(Integer id,Brand brand) {
        boolean update = this.update(brand,new UpdateWrapper<Brand>().eq("id",id));
        if (update) {
            return new Result(true, 200, "更新成功", null);
        } else {
            return new Result(false, 400, "更新失败", null);
        }
    }

    //findById
    @Override
    public Result findById(Integer id) {

        Brand byId = this.getById(id);

        return new Result(true, 200, "查询成功", byId);

    }

    @Override
    public Result findPage(Integer page, Integer size) {

        IPage<Brand> brandPage = new Page<>(page, size);
        this.page(brandPage, new QueryWrapper<Brand>());
        return new Result(true, StatusCode.OK, null,
                new PageResult<Brand>(brandPage.getTotal(),
                        brandPage.getRecords()));
    }

    @Override
    public Result findPage(Integer page, Integer size, Object searchMap) {
        IPage<Brand> iPage = null;
        try {
            String searchMapString = JSON.toJSONString(searchMap);
            Brand brand = JSON.parseObject(searchMapString, Brand.class);
            iPage = new Page<>(page, size);
            this.page(iPage, new QueryWrapper<Brand>()
                    .like(!StringUtils.isEmpty(brand.getName()), "name", brand.getName())
                    .like(!StringUtils.isEmpty(brand.getImage()), "image", brand.getImage())
                    .like(!StringUtils.isEmpty(brand.getLetter()), "letter", brand.getLetter())
                    .like(!StringUtils.isEmpty(brand.getSeq()), "seq", brand.getSeq()));
            return new Result(true, StatusCode.OK, "查询成功", new PageResult<Brand>(iPage.getTotal(), iPage.getRecords()));
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

//    @Override
//    public Result updateBrand(Brand brand, Integer id) {
////        if (brand.getName().equal(new QueryWrapper<Brand>().eq("name",brand.getName())) && brand.getName().isEmpty()) {
//            this.update(brand, new UpdateWrapper <Brand> ().eq("id", id));
////        UpdateWrapper<Brand> updateWrapper = new UpdateWrapper<>();
////        updateWrapper.eq("id", id);
//
//        //可将指定字段更新为null
////        updateWrapper.set("id", null);
////        updateWrapper.set("product_id", 123456);
//////        warehouseItemService.update(warehouseItem, updateWrapper);
//            return new Result(true, 200, "添加成功", brand);
////        } else {
////            return new Result(false, 400, "添加失败，名字重复或为空", null);
////        }
//    }
}
